//
//  VRUserGroupViewController.m
//  VRFirefighting
//
//  Created by Veer on 2018/8/19.
//  Copyright © 2018年 Veer. All rights reserved.
//

#import "VRUserGroupViewController.h"
#import "VRAddUserGroupViewController.h"

#import "VRUserGroupCell.h"
#import "VRContactModel.h"

@interface VRUserGroupViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) VRContactModel *mainContactModel;
@property (nonatomic, strong) NSArray<VRContactModel *> *contactArray;

@end

@implementation VRUserGroupViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = ScrollViewBgColor;
    self.title = @"用户组";
    
    self.mainContactModel = [[VRContactModel alloc] init];
    self.mainContactModel.fName = self.nameStr;
    self.mainContactModel.fPhone = self.mobile;
    [self setupSubView];
//    [self getUserGroup];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self getUserGroup];
}

- (void)setupSubView
{
    [self.view addSubview:self.tableView];
    
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 56)];
    self.tableView.tableFooterView = footerView;
    
    XButton *viewMonitorBtn = [XButton buttonWithType:UIButtonTypeCustom];
    //    goCardPackageBtn.layer.cornerRadius = 2;
    //    goCardPackageBtn.layer.borderColor = RGB(248, 177, 90).CGColor;
    viewMonitorBtn.layer.borderColor = MAINColor.CGColor;
    viewMonitorBtn.layer.borderWidth = 1.0;
    [viewMonitorBtn setImage:[UIImage imageNamed:@"add_fami"] forState:UIControlStateNormal];
    viewMonitorBtn.imageView.contentMode = UIViewContentModeScaleAspectFit;
    [viewMonitorBtn setTitle:@"添加新用户" forState:UIControlStateNormal];
    viewMonitorBtn.titleLabel.font = VRFont(14);
    [viewMonitorBtn setTitleColor:MAINColor forState:UIControlStateNormal];
    [viewMonitorBtn setTitleColor:RGB(211, 209, 209) forState:UIControlStateDisabled];
    [viewMonitorBtn addTarget:self action:@selector(onAddBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    viewMonitorBtn.imageEdgeInsets = UIEdgeInsetsMake(12, 0, 12, 0);
    [footerView addSubview:viewMonitorBtn];
    [viewMonitorBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(footerView);
        make.centerX.mas_equalTo(footerView);
        make.width.mas_equalTo(103);
        make.height.mas_equalTo(36);
    }];
}

#pragma mark - networking
- (void)getUserGroup
{
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"userID"] = [VRUser shareUser].token;
    //    param[@"fIsAreaManage"] = @([VRUser shareUser].fIsAreaManage);
    
    weakify(self)
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    //    hud.bezelView.backgroundColor = RGBA(0, 0, 0, 0.9);
    //    hud.bezelView.style = MBProgressHUDBackgroundStyleBlur;
    hud.mode = MBProgressHUDModeIndeterminate;
    [JKNetworking postWithSign:GetUserGroupList parameters:param success:^(NSDictionary *responseObject) {
        
        strongify(self)
        NSLog(@"responseObject:%@", responseObject);
        NSInteger code = [responseObject[@"state"] integerValue];
        NSString *msg = responseObject[@"message"];
        
        if (code == 0) {
            
            self.contactArray = [VRContactModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"]];
            dispatch_async(dispatch_get_main_queue(), ^{
                
                [self.tableView reloadData];
                [hud hideAnimated:YES];
            });
        }else if(msg.length > 0){
            dispatch_async(dispatch_get_main_queue(), ^{
                [SVProgressHUD showErrorWithStatus:msg];
                [hud hideAnimated:YES];
            });
        }
        [self.tableView.mj_header endRefreshing];
    } error:^(NSError * _Nonnull error) {
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [SVProgressHUD showErrorWithStatus:@"未知错误"];
            [hud hideAnimated:YES];
            [self.tableView.mj_header endRefreshing];
        });
    }];
}


#pragma mark - action
- (void)onAddBtnClick:(UIButton *)sender
{
    VRAddUserGroupViewController *addVc = [[VRAddUserGroupViewController alloc] init];
    [self.navigationController pushViewController:addVc animated:YES];
}

#pragma mark - delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (self.contactArray.count == 0) {
        return 1;
    }
    return 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 10;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *footerView = [[UIView alloc] init];
    return footerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 35;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 35)];
    headerView.backgroundColor = [UIColor whiteColor];
    
    NSString *imgName = section == 0 ? @"user_family" : @"user_main";
    NSString *title = section == 0 ? @"主用户" : @"家属用户";
    UIImageView *iconImgView = [[UIImageView alloc] init];
    iconImgView.image = [UIImage imageNamed:imgName];
    iconImgView.contentMode = UIViewContentModeScaleAspectFit;
    iconImgView.frame = CGRectMake(20, 35 - 18, 18, 18);
//    iconImgView.centerY = 21;
    [headerView addSubview:iconImgView];
    
    UILabel *nameLbl = [[UILabel alloc] init];
    nameLbl.text = title;
    nameLbl.textColor = MAINColor;
    nameLbl.font = VRFont(16);
    nameLbl.frame = CGRectMake(CGRectGetMaxX(iconImgView.frame) + 4, 35 - 18, 150, 18);
    [headerView addSubview:nameLbl];
    
    return headerView;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }
    return self.contactArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 80;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    VRContactModel *model = nil;
    if (indexPath.section == 0) {
        model = self.mainContactModel;
    }else{
        model = self.contactArray[indexPath.row];
    }
    
    VRUserGroupCell *cell = [VRUserGroupCell cellWith:tableView];
    cell.model = model;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

#pragma mark - lazy
- (UITableView *)tableView
{
    if (_tableView == nil) {
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = ScrollViewBgColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableHeaderView = [[UIView alloc] init];
        
        UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 0.01)];
        _tableView.tableHeaderView = view1;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.sectionFooterHeight = 0.01;
        [self.view addSubview:self.tableView];
        
        _tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
        //        [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        //            make.left.right.bottom.mas_equalTo(self.view);
        //            make.top.mas_equalTo(0);
        //        }];
    }
    return _tableView;
}

@end
